﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using Win32Wrapper;
using Models;
using System.IO;
using Common;

namespace WindowManagerService
{
    public class WindowsManager
    {
        private List<PokerWindow> _pokerWindows = null;

        public List<PokerWindow> PokerWindows
        {
            get
            {
                _pokerWindows = new List<PokerWindow>();
                GetListsOfWindows();
                return _pokerWindows;
            }
        }

        public string GetScreenFromWindow(PokerWindow pokerWindow)
        {
            string result = string.Empty;


            Bitmap image = GetWindowCaptureAsBitmap(pokerWindow.ID);
            result = SaveImageToFolder(image);

            return result;

        }

        private string SaveImageToFolder(Bitmap bitmap)
        {
            string directoryName = string.Concat(DateTime.Now.ToShortDateString(), " - ", DateTime.Now.ToLongTimeString(), " ", Guid.NewGuid().ToString()).Replace(".", "_").Replace(":", "_");
            DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(Path.Combine(Configuration.WindowScreenPath, directoryName));
            string fileName = string.Concat(ImageNames.TableWindow);
            string path = Path.Combine(directoryInfo.FullName, fileName);
            bitmap.Save(path, ImageFormat.Tiff);
            return directoryName;
        }

        private static Bitmap GetWindowCaptureAsBitmap(int handle)
        {
            IntPtr hWnd = new IntPtr(handle);
            Win32.Rect rc = new Win32.Rect();
            if (!Win32.GetWindowRect(hWnd, ref rc))
                return null;

            //			Win32.WindowInfo wi = new Win32.WindowInfo();
            //			wi.size = Marshal.SizeOf(wi);
            //			if (!Win32.GetWindowInfo(hWnd, ref wi))
            //				return null;

            // create a bitmap from the visible clipping bounds of the graphics object from the window
            Bitmap bitmap = new Bitmap(rc.Width, rc.Height);

            // create a graphics object from the bitmap
            Graphics gfxBitmap = Graphics.FromImage(bitmap);

            // get a device context for the bitmap
            IntPtr hdcBitmap = gfxBitmap.GetHdc();

            // get a device context for the window
            IntPtr hdcWindow = Win32.GetWindowDC(hWnd);

            // bitblt the window to the bitmap
            Win32.BitBlt(hdcBitmap, 0, 0, rc.Width, rc.Height, hdcWindow, 0, 0, (int)Win32.TernaryRasterOperations.SRCCOPY);

            // release the bitmap's device context
            gfxBitmap.ReleaseHdc(hdcBitmap);

            Win32.ReleaseDC(hWnd, hdcWindow);

            // dispose of the bitmap's graphics object
            gfxBitmap.Dispose();

            // return the bitmap of the window

            return bitmap;
        }

        private void GetListsOfWindows()
        {
            Win32.EnumWindowEventHandler callbackProc = new Win32.EnumWindowEventHandler(EnumWindows);
            Win32.EnumWindows(callbackProc, 0);
        }

        private bool EnumWindows(IntPtr hwnd, int lParam)
        {
            string windowsTitle = GetWindowText(hwnd);
            if (windowsTitle.Contains(Common.Configuration.WindowTitleKey) && !windowsTitle.EndsWith("**THM**"))
            {
                _pokerWindows.Add(new PokerWindow() { ID = hwnd.ToInt32(), Title = windowsTitle });
            }

            return true;
        }

        public string GetWindowText(IntPtr hWnd)
        {
            StringBuilder sb = new StringBuilder(256);
            Win32.GetWindowText(hWnd, sb, 256);
            return sb.ToString();
        }

        public string GetClassName(IntPtr hWnd)
        {
            StringBuilder sb = new StringBuilder(256);
            Win32.GetClassName(hWnd, sb, 256);
            return sb.ToString();
        }
    }
}
